<?php
namespace Admin\Controller;
use Think\Controller;
class LoginController extends Controller {
    public function login(){
    	if(session('user')){
			$this->redirect('Index/index');
		}
        $this-> display();
    }

    public function doLogin(){

    	$user = M('user');
    	$login = I('post.');
    	$name = I('post.name');
    	$pass = md5(I('post.pass'));
    	$data = $user -> where('name = "'.$name.'"') -> find();
    	//验证验证码
    	$verify = new \Think\Verify();
    	if(!$verify -> check($login['code'])){
    		echo "<script>
                alert('验证码输入错误');
                window.location.href='".U('Admin/Login/login')."';
                </script>";
                die();
    	}
    	//验证用户名
    	if(empty($data)){
    		echo "<script>
				alert('用户名错误！')
				window.location.href='".U('Admin/Login/login')."';
    		</script>";
    		die;
    	}
    	//验证密码
    	if($pass != $data['pass']){
    		echo "<script>
				alert('密码错误！')
				window.location.href='".U('Admin/Login/login')."';
    		</script>";
    		die;
    	}
    	//是否禁用
    	if($data['status'] != 1){
    		echo "<script>
                alert('此管理员已被禁用');
                window.location.href='".U('Admin/Login/login')."';
                </script>";
                die();
    	}

    	unset($data['pass']);
    	session('user',$data);
    	$da['listtime'] = time();
    	$da['id'] = $data['id'];
    	$user -> save($da);

    	$this -> success('登录成功',U('Admin/Index/index'),1);
    }

    public function loginOut(){
    	session('user',null);
        $this->redirect('Login/login');
    }
}